package com.bw.good.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bawei.good.databinding.ItemGoodLayoutBinding
import com.bawei.good.databinding.ItemGoodLiveLayoutBinding
import com.bawei.image.ImgOptions
import com.bawei.image.LoadImageUtil
import com.bw.good.entity.GoodEntity

/**
 * 商品列表适配器
 */
class GoodLiveAdapter constructor(private val list: MutableList<GoodEntity>): RecyclerView.Adapter<GoodLiveAdapter.GoodViewHolder>() {

    private var itemClickListener: GoodItemClickListener?= null

    fun setItemClickListener(listener: GoodItemClickListener) {
        itemClickListener = listener
    }

    inner class GoodViewHolder(val dataBinding: ItemGoodLiveLayoutBinding): RecyclerView.ViewHolder(dataBinding.root)

    override fun onBindViewHolder(holder: GoodViewHolder, position: Int) {
        val entity = list[position]
        entity?.goods_default_icon?.let {
            LoadImageUtil.loadUrl(
                holder.dataBinding.root.context,
                it,
                holder.dataBinding.ivGoodIcon,
                ImgOptions.Builder.isCircle(false).build()
            )
        }
        holder.dataBinding.tvGoodDesc.text = entity?.goods_desc
        holder.dataBinding.tvGoodPrice.text = entity?.goods_default_price.toString()

        //设置点击事件
        holder.dataBinding.ivGoodIcon.setOnClickListener {
            itemClickListener?.onItemClickChildClick(it, position, entity!!)
        }
    }

    override fun getItemCount(): Int = list.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodViewHolder {
        return GoodViewHolder(ItemGoodLiveLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    interface GoodItemClickListener{
        fun onItemClickChildClick(v: View, position: Int, entity: GoodEntity)
    }
}